package com.zp.base.impl.labMaintenance;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zp.base.entity.labMaintenance.LabType;
import com.zp.base.entity.labMaintenance.bo.LabTypeBo;
import com.zp.base.entity.labMaintenance.bo.LabVsClinicBo;
import com.zp.base.entity.labMaintenance.vo.LabTypeVo;
import com.zp.base.entity.labMaintenance.vo.LabVsClinicVo;
import com.zp.base.mapper.labMaintenance.LabTypeMapper;
import com.zp.base.service.labMaintenance.ILabTypeService;
import com.zp.base.service.labMaintenance.ILabVsClinicService;
import com.zp.common.core.utils.StringUtils;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 检验项目类型lab_typeService业务层处理
 *
 * @author zhang peng
 * @ date 2023-05-29
 */
@RequiredArgsConstructor
@Service
public class LabTypeServiceImpl implements ILabTypeService {

    private final LabTypeMapper baseMapper;

    private final ILabVsClinicService labVsClinicService;

    /**
     * 查询检验项目类型lab_type
     */
    @Override
    public LabTypeVo queryById(String labTypeId) {
        return baseMapper.selectVoById(labTypeId);
    }

    /**
     * 查询检验项目类型lab_type列表(分页)
     */
    @Override
    public TableDataInfo<LabTypeVo> queryPageList(LabTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LabType> lqw = buildQueryWrapper(bo);
        Page<LabTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询检验项目类型lab_type列表(不分页)
     */
    @Override
    public List<LabTypeVo> queryList(LabTypeBo bo) {
        LambdaQueryWrapper<LabType> lqw = buildQueryWrapper(bo);
        List<LabTypeVo> labTypeVos = baseMapper.selectVoList(lqw);
        LabVsClinicBo labVsClinicBo = new LabVsClinicBo();
        labVsClinicBo.setOrgId(bo.getOrgId());
        labVsClinicBo.setDelFlag("0");
        List<LabVsClinicVo> labVsClinicVos = labVsClinicService.queryList(labVsClinicBo);
        labTypeVos.forEach(labTypeVo -> {
            List<LabVsClinicVo> collect = labVsClinicVos.stream().filter(x -> StringUtils.equals(x.getLabId(), labTypeVo.getLabTypeId())).collect(Collectors.toList());
            labTypeVo.setLabVsClinicVoList(collect);
        });
        return labTypeVos;
    }

    private LambdaQueryWrapper<LabType> buildQueryWrapper(LabTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LabType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getLabTypeId()), LabType::getLabTypeId, bo.getLabTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), LabType::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getOrgId()), LabType::getOrgId, bo.getOrgId());
        lqw.eq(bo.getLevel() != null, LabType::getLevel, bo.getLevel());
        lqw.eq(bo.getSort() != null, LabType::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getPerformedBy()), LabType::getPerformedBy, bo.getPerformedBy());
        lqw.eq(StringUtils.isNotBlank(bo.getParentId()), LabType::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getParentIds()), LabType::getParentIds, bo.getParentIds());
        lqw.eq(StringUtils.isNotBlank(bo.getRemarks()), LabType::getRemarks, bo.getRemarks());
        lqw.orderByAsc(LabType::getSort);
        return lqw;
    }

    /**
     * 新增检验项目类型lab_type
     */
    @Override
    public Boolean insertByBo(LabTypeBo bo) {
        LabType add = BeanUtil.toBean(bo, LabType.class);
        validEntityBeforeSave(add);
        add.initEntity();
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setLabTypeId(add.getLabTypeId());
        }
        return flag;
    }

    /**
     * 修改检验项目类型lab_type
     */
    @Override
    public Boolean updateByBo(LabTypeBo bo) {
        LabType update = BeanUtil.toBean(bo, LabType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LabType entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除检验项目类型lab_type
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
